package com.lyz.dataStructure.DesignModel;/**
 * @Author:759057893@qq.com Lyz
 * @Date: ${Date} 22:38
 * @Description:
 **/

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 *@Author:759057893@qq.com Lyz
 *@Date: 2019/7/12 22:38
 *@Description:
 **/
public interface Subject {

    public void display();
}

class  RealSubject implements Subject{

    @Override
    public void display() {
        System.out.println("展示！");
    }
}

class MyInvocationHandler implements InvocationHandler{

    Subject realSubjecct;
    public  MyInvocationHandler(Subject realSubjecct){
        this.realSubjecct=realSubjecct;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        Object result =null;
        if(method.getName().equals("display")){
            result =method.invoke(realSubjecct,args);
        }
        return result;
    }
}

class  Client{
    public static void main(String[] args) {
        //真实对象
        Subject realSubject = new RealSubject();

        MyInvocationHandler my = new MyInvocationHandler(realSubject);

        //代理对象
        Subject proxyClass = (Subject)Proxy.newProxyInstance(ClassLoader.getSystemClassLoader(),
                new Class[]{Subject.class},my);
        proxyClass.display();
   }
}
